package com.example.rpc_core.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@Slf4j
public class MockProxy implements InvocationHandler {


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //根据返回值类型返回一个mock对象
        Class<?> returnType = method.getReturnType();
        log.info("returnType:{}",returnType);
        return getDafaultValue(returnType);
    }

    /**
     * 更具返回值创建一个默认值
     * @param returnType
     * @return
     */
    private Object getDafaultValue(Class<?> returnType) {
        if (returnType.isPrimitive()) {
            if (returnType == int.class) {
                return 0;
            } else if (returnType == boolean.class) {
                return false;
            } else if (returnType == char.class) {
                return ' ';
            } else if (returnType == byte.class) {
                return (byte) 0;
            } else if (returnType == short.class) {
                return (short) 0;
            } else if (returnType == long.class) {
                return (long) 0;
            } else if (returnType == float.class) {
                return (float) 0;
            } else if (returnType == double.class) {
                return (double) 0;
            }
        }
            return null;
    }
}
